package com.yinp.proapp.module.game.fourcolormap;

import android.graphics.Color;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import com.yinp.proapp.R;
import com.yinp.proapp.base.activity.AppBaseActivity;
import com.yinp.proapp.databinding.ActivityFourColorMapBinding;
import com.yinp.proapp.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: yinp
 * @Date: 2021/9/16
 * @Description:四色地图游戏
 */
public class FourColorMapActivity extends AppBaseActivity<ActivityFourColorMapBinding> {
    private List<TextView> list = new ArrayList<>();

    @Override
    protected void initViews() {
        setStatusBarHeight(StatusBarUtil.getStatusBarHeight(mContext));
        initClick(bd.header.headerBackImg, bd.tvBlue, bd.tvGreen, bd.tvRed, bd.tvWhite, bd.tvYellow, bd.stvRestart);

        list.add(bd.tvRed);
        list.add(bd.tvYellow);
        list.add(bd.tvBlue);
        list.add(bd.tvGreen);
        list.add(bd.tvWhite);
    }

    boolean[] isSelected = {false, false, false, false, false};

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == bd.header.headerBackImg) {
            finish();
        } else if (v == bd.tvRed) {
            if (!isSelected[0]) {
                closeOpenAnimation();
                selectAnimation(list.get(0));
                isSelected[0] = true;
                bd.mvMain.setColor(Color.RED);
            }
        } else if (v == bd.tvYellow) {
            if (!isSelected[1]) {
                closeOpenAnimation();
                selectAnimation(list.get(1));
                isSelected[1] = true;
                bd.mvMain.setColor(Color.YELLOW);
            }
        } else if (v == bd.tvBlue) {
            if (!isSelected[2]) {
                closeOpenAnimation();
                selectAnimation(list.get(2));
                isSelected[2] = true;
                bd.mvMain.setColor(Color.BLUE);
            }
        } else if (v == bd.tvGreen) {
            if (!isSelected[3]) {
                closeOpenAnimation();
                selectAnimation(list.get(3));
                isSelected[3] = true;
                bd.mvMain.setColor(Color.GREEN);
            }
        } else if (v == bd.tvWhite) {
            if (!isSelected[4]) {
                closeOpenAnimation();
                selectAnimation(list.get(4));
                isSelected[4] = true;
                bd.mvMain.setColor(Color.WHITE);
            }
        } else if (v == bd.stvRestart) {
            bd.mvMain.restart();
        }
    }


    //关闭已经打开的动画
    public void closeOpenAnimation() {
        for (int i = 0; i < isSelected.length; i++) {
            if (isSelected[i]) {
                cancelAnimation(list.get(i));
                isSelected[i] = false;
            }
        }
    }

    //选中以后的动画效果，放大
    public void selectAnimation(TextView tv) {
        Animation anim = AnimationUtils.loadAnimation(this, R.anim.bg_scale_amplification);
        tv.startAnimation(anim);
    }

    //将放大的效果复原
    public void cancelAnimation(TextView tv) {
        Animation anim = AnimationUtils.loadAnimation(this, R.anim.bg_scale_shrink);
        tv.startAnimation(anim);
    }
}